<template>
  <div>
    <div>{{ $t('test') }}</div>
    <button @click="handleSwitchLang('en')">{{ 'English' }}</button>
    <button @click="handleSwitchLang('cn')">{{ '简体中文' }}</button>
    <Button type="primary">2</Button>
    {{ $store.state.contract.data.markPrice }}
  </div>
</template>

<script>
  import { mapState } from 'vuex'
  import { CONTRACT as type, SET_MARK_PRICE } from "@/store/types";
  import {localStoragePrefix} from '@/config'
  import { wsURL } from "@/config";
  import socket from '@/utils/websocket'

  const CONTRACT_SET_MARK_PRICE = `${type}/${SET_MARK_PRICE}`

  function catError() {
  	return function (target, name, descriptor) {
		  const oldValue = descriptor.value
		  descriptor.value = function () {
			  try {
			  	console.log(this)
				  oldValue.apply(this, arguments)
			  } catch (e) {
				  console.log(e)
			  }

		  }
	  }

  }

	export default {
		name: "test",
    data() {
			return {
				test: 0
      }
    },
		mounted() {
			this.$nextTick(() => {

			})
			// this.init()
      this.withError()
		},
    computed: {
			...mapState('contract' ,{
				wsStatus: state => state.wsStatus
      })
    },
    watch: {
			wsStatus() {
				//console.log(this.wsStatus)
      },
      test() {
				console.log('改变了')
      }
    },
		methods: {
			@catError()
			withError() {
				throw new Error('2')
      },
			init() {
				const callback = (res) => {
          const data = JSON.parse(res)
          if(data.channel === 'mark') {
          	this.$store.commit(CONTRACT_SET_MARK_PRICE, data.data.price)
          }
        }
				const ws = new socket({
					wsURL,
          callback,
          type
				})
				ws.init()

        // setTimeout(() => {
        // 	ws.sendMessage(JSON.stringify({
		    //     args: ['mark:btc_usdt'],
		    //     op: 'unsubscribe'
	      //   }))
        //   ws.handleClose()
        // }, 1000 * 10)
			},
			handleSwitchLang(targetLang) {
				this.$i18n.locale = targetLang
				localStorage.setItem(localStoragePrefix, targetLang)
			}
		}
	}
</script>

<style scoped>

</style>
